package com.example.everying.framework.websocket.handler;

import com.alibaba.fastjson2.JSONObject;
import com.example.everying.constant.CacheConstants;
import com.example.everying.framework.websocket.service.WebSocketChatService;
import com.example.everying.model.dto.system.NotificationMessage;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
public class RedisMessageReceiver implements MessageListener {

    private final ObjectMapper objectMapper;

    public RedisMessageReceiver(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    @PostConstruct
    public void init() {
        log.info("Redis 消息接收器已启动，监听频道:{}", CacheConstants.NOTIFICATION_KEY);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        try {
            byte[] body = message.getBody();
            String rawJson = new String(body, StandardCharsets.UTF_8);
            log.info("收到消息内容: {}", rawJson);

            NotificationMessage notification = objectMapper.readValue(rawJson, NotificationMessage.class);
            if (notification.getType() == null) {
                log.warn("消息类型为空");
                return;
            }

            switch (notification.getType()) {
                case SYSTEM:
                    handleSystemNotification(notification);
                    break;
                case OFFICIAL:
                    handleOfficialNotification(notification);
                    break;
                case NOTICE_UNREAD_STATUS:
                    handleNoticeUnreadStatus(notification);
                    break;
                default:
                    log.warn("未知消息类型");
            }
        } catch (Exception e) {
            log.error("处理消息失败", e);
        }
    }

    private void handleSystemNotification(NotificationMessage message) {
        JSONObject notification = new JSONObject();
        notification.put("type", "notification");
        notification.put("content", message.getContent());
        WebSocketChatService.localBroadcast(JSONObject.toJSONString(notification));
        log.info("处理系统通知: {}", message.getTitle());
    }

    private void handleOfficialNotification(NotificationMessage message) {
        JSONObject notification = new JSONObject();
        notification.put("type", "OfficialNotification");
        notification.put("content", message.getContent());
        WebSocketChatService.localBroadcast(JSONObject.toJSONString(notification));
        log.info("处理官方通知: {}", message.getTitle());
    }

    private void handleNoticeUnreadStatus(NotificationMessage message) {
        JSONObject notification = new JSONObject();
        notification.put("type", "NoticeUnreadStatus");
        notification.put("content", message.getContent());
        WebSocketChatService.localBroadcast(JSONObject.toJSONString(notification));
        log.info("处理官方通知未读状态: {}", message.getTitle());
    }
}

